home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17320 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  53 lines

  1. Path: baggins.cc.flinders.edu.au!usenet
  2. From: "T. Gruber" <gruber@es.flinders.edu.au>
  3. Newsgroups: comp.lang.c++
  4. Subject: Why?  Virtual member causes General Protection Exception
  5. Date: Mon, 15 Apr 1996 19:21:38 -0700
  6. Organization: Flinders University    
  7. Message-ID: <31730432.28C@es.flinders.edu.au>
  8. NNTP-Posting-Host: geophone.es.flinders.edu.au
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (Win16; I)
  13.  
  14. Hi
  15.  
  16. I can compile and run the appended code (BC++ 4.5; Win 3.11), but it causes a 
  17. General Protection Exception in the last line. Why? 
  18.  
  19. A::y() does call B::x(), but this code does not get the right address for B::i. I 
  20. assume I am at fault, but I can't find a documented reference that tells me so. Am 
  21. *I* stupid?
  22.  
  23. Please email a copy of your reply.
  24.  
  25. Thanks
  26.  
  27. Thomas
  28.  
  29. #include <iostream.h>
  30.  
  31. class A {
  32. public:
  33.   virtual void x() = 0;
  34.   void y() { x(); };
  35. };
  36.  
  37. class B : public virtual A {
  38. public:
  39.   B() : i(0) {};
  40.   virtual void x() { cout << i << endl; };
  41.   int i;
  42. };
  43.  
  44. class C : public virtual A, public virtual B {};
  45.  
  46. void main()
  47. {
  48.   C c1;
  49.   C c2(c1);
  50.   c2.x();  // works
  51.   c2.y();  // crashes in B::x()
  52. }
  53.